package org.activequant.data.retrieval.dao;

import org.activequant.core.domainmodel.SeriesSpecification;
import org.activequant.core.domainmodel.data.TickerNewsSeries;
import org.activequant.dao.ITickerNewsDao;
import org.activequant.data.retrieval.ITickerNewsSeriesSource;
import org.apache.log4j.Logger;

/**
* TickerNews series source that fetches data from the database.<br/>
* An ITickerNewsSeriesSource implementation. Holds the following associated variables:
* <ul>
* <li>dao(ITickerNewsDao)</li>
* </ul>
*/
public class DaoTickerNewsSeriesSource implements ITickerNewsSeriesSource {

	private final Logger log = Logger.getLogger(getClass());
	/**
	 * private ITickerNewsDao dao;
	 */
	private ITickerNewsDao dao;
	/**
	 * returns the associated dao(ITickerNewsDao)
	 * @return
	 */
	public ITickerNewsDao getDao() {
		return dao;
	}
	/**
	 * sets the associated dao(ITickerNewsDao) with the given val(ITickerNewsDao)
	 * @param val
	 */
	public void setDao(ITickerNewsDao val) {
		dao = val;
	}
	/**
	 * returns a TickerNewsSeries whose associated seriesSpecification(SeriesSpecification) is set with the given specs(SeriesSpecification).<br/>
	 * Sets its TickerNews elements by fetching the TickerNews(s) for the given specs(SeriesSpecification) from the associated dao(ITickerNewsDao).
	 */
	public TickerNewsSeries fetch(SeriesSpecification specs) throws Exception {
		if(dao == null) {
			throw new IllegalStateException("dao property not set");
		}
		TickerNewsSeries series = new TickerNewsSeries(specs);
		log.debug("populating series from the data source: " + specs);
		series.setTickerNews(dao.findBySeriesSpecification(specs));
		return series;
	}
	/**
	 * returns the simple name of this class
	 * @return
	 */
	public String getVendorName() {
		return getClass().getSimpleName();
	}
}
